package com.ea.controller.common;

import com.ea.common.enums.OperationResultCode;
import com.ea.common.exception.ServiceException;
import com.ea.common.response.JsonResult;
import com.ea.common.util.TypeConversionToJson;
import com.ea.common.util.tree.TreeNode;
import com.ea.domain.dto.ElectricGoodsSalesTotalDTO;
import com.ea.domain.dto.SysCfgUpdateDTO;
import com.ea.domain.dto.SysDicUpdateDTO;
import com.ea.domain.dto.TotalSaleDTO;
import com.ea.domain.entity.AreaInfo;
import com.ea.domain.entity.SysBaseDictionary;
import com.ea.domain.query.TotalSaleQuery;
import com.ea.service.ICommonService;
import com.ea.service.IOrderFormDetailService;
import com.ea.service.IOrderInfoService;
import com.ea.service.ISysBaseCfgService;
import com.ea.service.ISysBaseDictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author NAME-L
 * @Description TODO
 * @className CommonController
 * @date 2023-01-31 22:43:49
 */
@RestController
@RequestMapping("/electric/common")
public class CommonController {

    @Autowired
    private ICommonService commonService;

    @Autowired
    private ISysBaseDictionaryService dictionaryService;

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private ISysBaseCfgService baseCfgService;

    @Autowired
    private IOrderFormDetailService detailService;


    @GetMapping("/list/province")
    public JsonResult queryProvince() {
        List<TreeNode<AreaInfo>> returnList = commonService.queryAllProvince();
        return JsonResult.querySuccessFul(returnList);
    }

    @GetMapping("/sys/dic/list")
    public JsonResult getElectricSysDicType(@RequestParam("type") String type) {
        if (StringUtils.isEmpty(type)) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        return JsonResult.querySuccessFul(dictionaryService.getSysDicList(type));
    }

    @PostMapping("/sys/dic/insert")
    public JsonResult insertElectricSysDic(@RequestBody String dicStr) {
        if (StringUtils.isEmpty(dicStr)) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        SysBaseDictionary dictionary = TypeConversionToJson.jsonToEntity(dicStr, SysBaseDictionary.class);
        dictionaryService.insertSysDic(dictionary);
        return JsonResult.doSuccessful();
    }

    @PostMapping("/sys/dic/update")
    public JsonResult updateElectricSysDic(@RequestBody String updateStr) {
        if (StringUtils.isEmpty(updateStr)) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        SysDicUpdateDTO dictionary = TypeConversionToJson.jsonToEntity(updateStr, SysDicUpdateDTO.class);
        dictionaryService.updateSysDic(dictionary);
        return JsonResult.doSuccessful();
    }

    @GetMapping("/sys/dic/del")
    public JsonResult delElectricSysDic(@RequestParam("dicId") Long dicId) {
        if (dicId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        dictionaryService.delSysDic(dicId);
        return JsonResult.doSuccessful();
    }

    @GetMapping("/sys/cfg/list")
    public JsonResult getElectricSysCfg() {
        return JsonResult.querySuccessFul(baseCfgService.getSysCfgList());
    }

    @PostMapping("/sys/cfg/update")
    public JsonResult updateElectricSysCfg(@RequestBody SysCfgUpdateDTO updateDTO) {
        baseCfgService.updateCfgSys(updateDTO);
        return JsonResult.updateSuccessFul();
    }

    @GetMapping("/home/data")
    public JsonResult getTodaySalesCount() {
        Map<String, String> homeData = commonService.getHomeData();
        return JsonResult.doSuccessful(homeData);
    }

    @PostMapping("/totalPrice/byTime")
    public JsonResult selectTotalPriceByTime(@RequestBody TotalSaleQuery query) {
        List<TotalSaleDTO> returnList = orderInfoService.selectTotalPriceByTime(query);
        return JsonResult.doSuccessful(returnList);
    }

    @GetMapping("/totalSales")
    public JsonResult queryTotalSales() {
        List<ElectricGoodsSalesTotalDTO> returnList = detailService.getTodayAllGoodsSale();
        return JsonResult.doSuccessful(returnList);
    }


}
